
import datetime
from app.db.models.asset_tasks import AssetTasks
from app.db.models.jobs import Job
from app.db.models.tasks import Task


class UiOptRollback:

    def service(self):
        """
        删除asset_task新增的数据；
        删除job.isSupportIpv6字段；
        删除job.securityStatus；
        删除asset.status, asset.lastResult字段;
        删除task.addition补充字段;
        """
        print(f"{datetime.datetime.now()} 开始回退数据......")
        # 删除asset_task新增的数据
        AssetTasks.objects.filter(taskGroupType__in=["http", "ping", "web_asset", "web_security"]).delete()
        # 删除 job.isSupportIpv6 字段
        Job.objects(isSupportIpv6__exists=True).update(unset__isSupportIpv6=1)
        Job.objects(securityStatus__exists=True).update(unset__securityStatus=1)
        # 删除 asset.status, asset.lastResult 字段
        AssetTasks.objects(status__exists=True).update(unset__status=1)
        AssetTasks.objects(lastResult__exists=True).update(unset__lastResult=1)
        # 还原ipv6任务taskGroupType为web
        AssetTasks.objects(taskGroupType="ipv6").update(taskGroupType="web")
        print(f"{datetime.datetime.now()} 回退结束")
